#!/bin/bash

set -e

dataMnt=/mnt/server.lan/data
dataStorage=$dataMnt/dataStorage

flag=false
if [ ! -e "$dataStorage" ];then
  echo "[E] $dataStorage : dir not exists,please make it"
  flag=true
fi


if $flag;then
  exit 1
fi

###################
## settings
name="bitwarden.server.lan"
# debian
# image="bitwardenrs/server"
image="vaultwarden/server"

mkDir() {
  if [ -z "$1" ]; then
    return
  fi

  if [ ! -e "${1}" ]; then
    mkdir -p "${1}"
    chown 1000:1000 "${1}"
  fi
}
dataDir="$dataStorage/$name"
mkDir "${dataDir}"


docker pull "${image}"

exit $?


if [ -z "$TZ" ];then
  TZ="Asia/Shanghai"
fi

        # docker run -d --restart=always \
        #     --name="${container_name}" \
        #     -v "${dataPath}":/data/ \
        #     -e TZ=$TZ \
        #     -v /usr/share/zoneinfo/$TZ:/etc/localtime:ro \
        #     -v /etc/timezone:/etc/timezone:ro \
        #     -e ROCKET_PORT=8080 \
        #     -e DOMAIN="https://bitwarden.server.lan" \
        #     ${EMAIL_ARGS_FOR_EMAIL} \
        #     -p 9000:8080 \
        #     bitwardenrs/server


# #TODO
# # EMAIL_ARGS_FOR_EMAIL="-e SMTP_HOST=<smtp.domain.tld> -e SMTP_FROM=<bitwarden@domain.tld> -e SMTP_PORT=587 -e SMTP_SSL=true -e SMTP_USERNAME=<username> -e SMTP_PASSWORD=<password> "
# EMAIL_ARGS_FOR_EMAIL=" "
# if [ -n "${EMAIL_SECRET}" ];then
#     EMAIL_ARGS_FOR_EMAIL=" -e SMTP_HOST=${EMAIL_HOST} -e SMTP_FROM=${EMAIL} -e SMTP_PORT=587 -e SMTP_SSL=true -e SMTP_USERNAME=${EMAIL} -e SMTP_PASSWORD=${EMAIL_SECRET}  -e SMTP_EXPLICIT_TLS=false "
# fi

# ## default port=ROCKET_PORT=80
# docker run -d --name "${name}" \
#     --restart=always \
#     -v "$dataDir":/data \
#     -e TZ=$TZ \
#     -p 127.0.0.1:9000:8080 \
#     -e ROCKET_PORT=8080 \
#     -e DOMAIN="https://bitwarden.server.lan" \
#     --user="${uid_}:${gid_}" \
#     ${EMAIL_ARGS_FOR_EMAIL} \
#     "${image}"



